<template>
  <el-container class="login-container">
    <el-card class="login-form">
      <h2>登录</h2>
      <el-form class="login-button-wrapper" ref="loginFormRef" :model="loginForm" :rules="rules"
               style="flex-direction: column">
        <el-form-item class="login-button-wrapper" label="工号" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入工号"></el-input>
        </el-form-item>
        <el-form-item class="login-button-wrapper" label="密码" prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入密码" type="password"></el-input>
        </el-form-item>
        <el-form-item class="login-button-wrapper">
          <el-button type="primary" @click="handleLogin" style="width: 100%; margin: 1vh 0;">
            <span class="center">登录</span>
          </el-button>
        </el-form-item>
        <div style="text-align: center; font-size: 12px; color: #666; margin-top: 10px;">
          推荐使用：谷歌浏览器、Edge浏览器、360安全浏览器（极速模式）
        </div>
      </el-form>
    </el-card>
  </el-container>
</template>

<script>
import {onMounted, ref} from 'vue'
import {useStore} from 'vuex'
import {ElMessage} from 'element-plus'

export default {
  setup() {
    const loginForm = ref({
      username: '',
      password: ''
    })
    const loginFormRef = ref(null)
    const store = useStore()

    const rules = {
      username: [{required: true, message: '请输入工号', trigger: 'blur'}],
      password: [{required: true, message: '请输入密码', trigger: 'blur'}]
    }

    const handleLogin = async () => {
      try {
        await loginFormRef.value.validate()
        await store.dispatch('handleLogin', {
          username: loginForm.value.username,
          password: loginForm.value.password
        })
        // const { username, password } = loginForm.value
        // store.commit('setUsername', username)
        // console.log(username, password)
        // if (username === '10040056') {
        //   store.commit('setToken', username)
        //   store.commit('setRole', 'normal') // 设置普通用户角色
        // } else if (username === '20090004') {
        //   store.commit('setToken', username)
        //   store.commit('setRole', 'admin') // 设置管理员角色
        // } else {
        //   ElMessage.error('用户名或密码错误')
        //   return
        // }
        // ElMessage.success('登录成功')
        // router.push('/home')
      } catch (error) {
        ElMessage.error('表单校验失败')
      }
    }

    onMounted(() => {
      const formItems = document.querySelectorAll('.el-form-item__content');
      formItems[2].style.margin = '1vh 5.5vw';
    });

    return {
      loginForm,
      rules,
      loginFormRef,
      handleLogin
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 98vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('@/../public/half_background.jpg');
  background-size: cover;
  background-position: center;
}

.login-form {
  width: 36vw;
  height: 50vh;
  padding: 1vw;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.login-button-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  margin-top: 20px;
}

.center {
  display: flex;
  justify-content: center;
  align-items: center;
}

.el-form {
  label-width: 5vw;
}

@media (max-aspect-ratio: 1/1) {
  .login-form {
    width: 60vw;
    height: 40vh;
    padding: 2vw;
  }

  .login-button-wrapper {
    margin-top: 10px;
  }

  .el-form-item__content {
    margin: 1vh 0;
  }

  h2 {
    font-size: 1.5rem;
  }

  .el-form-item__label {
    font-size: 0.9rem;
  }

  .el-input__inner {
    font-size: 0.9rem;
  }

  .el-button {
    font-size: 0.9rem;
  }

  .el-form {
    label-width: 11vw;
  }
}
</style>
